<template>
  <el-card shadow="hover">
    <el-button @click="addUI" class="left" type="success">添加</el-button>
    <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" row-key="permissionId" border>
      <el-table-column align="center" prop="empId" label="用户id" sortable></el-table-column>
      <el-table-column align="center" prop="empName" label="用户昵称" sortable></el-table-column>
      <el-table-column align="center" prop="empUacc" label="账号" sortable></el-table-column>
      <el-table-column align="center" prop="empPhone" label="联系电话" sortable></el-table-column>
      <el-table-column align="center" prop="empPhoto" label="照片" width="100">
        <template slot-scope="scope">
          <el-image
              style="width: 50px; height: 50px;font-size: 12px"
              :src="getPhotot(scope.row)"
              fit="cover"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
            <el-button type="danger" @click="del(scope.row)" icon="el-icon-delete" size="mini"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="dark" content="编辑" placement="bottom">
            <el-button type="warning" @click="updateUI(scope.row)" icon="el-icon-edit" size="mini"></el-button>
          </el-tooltip>

          <el-tooltip class="item" effect="dark" content="分配角色" placement="bottom">
            <el-button type="primary" @click="distRoleToEmp(scope.row)" icon="el-icon-key" size="mini"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="用户" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="昵称" :label-width="formLabelWidth">
          <el-input v-model="form.empName" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="联系电话" :label-width="formLabelWidth">
          <el-input v-model="form.empPhone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="照片" :label-width="formLabelWidth">
          <el-upload
              class="avatar-uploader my-avatar"
              action="http://localhost:8080/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.page"
                   :page-sizes="[5, 10, 15, 20]" :page-size="pageInfo.size"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="pageInfo.total"></el-pagination>
  </el-card>
</template>

<script>
import {findAll, doSaveOrUpdate, deleteByEmpId} from "@/api/emp/index.js";

export default {
  mounted() {
    this.updateTable();
  },
  data() {
    return {
      tableData: [],
      fileList: [],
      form: {
        empUacc: undefined,
        empName: undefined,
        empPhone: undefined,
        empPhoto: undefined
      },
      dialogFormVisible: false,
      formLabelWidth: "180px",
      pageInfo: {
        total: 0,
        page: 1,
        size: 5
      },
      imageUrl: ''
    };
  },
  methods: {
    getPhotot(row) {
      return `http://localhost:8080/download?fileName=${row.empPhoto}`
    },
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.imageUrl = `http://localhost:8080/download?fileName=${res.data}`;
      this.form.userPhoto = res.data
    },
    beforeAvatarUpload(file) {
      const isPNG = file.type === 'image/png';
      const isJPEG = file.type === 'image/jpeg';
      const isGIF = file.type === 'image/gif';
      const isLt5M = file.size / 1024 / 1024 < 5;

      if (!(isPNG || isJPEG || isGIF)) {
        this.$message.error('上传头像图片只能是 PNG、JPEG 或 GIF 格式!');
      }
      if (!isLt5M) {
        this.$message.error('上传头像图片大小不能超过 5MB!');
      }
      return (isPNG || isJPEG || isGIF) && isLt5M;
    },
    handleCurrentChange(page) {
      this.pageInfo.page = page;
      this.updateTable();
    },
    handleSizeChange(size) {
      this.pageInfo.size = size;
      this.updateTable();
    },
    updateTable() {
      findAll(this.pageInfo).then(r => {
        this.tableData = r.data.list;
        this.pageInfo.total = r.data.total;
      });
    },
    addUI() {
      (this.form = {}), (this.dialogFormVisible = true);
    },
    handlePreview() {

    },
    handleRemove() {
    },
    del(row) {
      this.$confirm("确认删除?").then(() => {
        let empId = row.empId;
        deleteByEmpId(empId).then(r => {
          if (r.code == 200) {
            this.updateTable();
            this.$message.success(r.msg);
            return;
          }
          this.$message.error(r.msg);
        });
      });
    },
    updateUI(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.imageUrl = this.getPhotot(row)
      this.dialogFormVisible = true;
    },
    saveOrUpdate() {
      doSaveOrUpdate(this.form).then(r => {
        if (r.code == 200) {
          this.dialogFormVisible = false;
          this.$message.success(r.msg);
          this.updateTable();
          this.fileList = [];
          this.imageUrl = ''
        }
      });
    },
    uploadSuccess(r) {
      this.form.userPhoto = r.data.newFileName;
    },
    distRoleToEmp(row) {
      this.$router.push(`DistRoleToEmp/${row.empId}`);
    }
  }
};
</script>

<style scoped lang="less">
.left {
  margin: 10px 0;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.my-avatar {
  height: 175px;
  line-height: 175px;
  width: 175px;
  border: 1px dashed #a31a1a;
  border-radius: 6px;
  cursor: pointer;
}

.el-icon-plus:before {
  content: "\e6d9";
  height: 175px;
  line-height: 175px;
  width: 175px;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  height: 175px;
  line-height: 175px;
  width: 175px;
}

.avatar {
  height: 175px;
  line-height: 175px;
  width: 175px;
  display: block;
}

.el-image {
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 50%;
}
</style>
